Skip to content

fix(security): truncate misaligned AES ciphertext instead of throwing#29

Merged
Mythie merged 2 commits intoLibPDF-js:mainfrom
ntedvs:fix/aes-misaligned-ciphertext
Feb 19, 2026
Merged

fix(security): truncate misaligned AES ciphertext instead of throwing#29
Mythie merged 2 commits intoLibPDF-js:mainfrom
ntedvs:fix/aes-misaligned-ciphertext

Conversation

@ntedvs
Copy link
Contributor

@ntedvs ntedvs commented Feb 17, 2026

Summary

Fixes #28

aesDecrypt threw when ciphertext length wasn't a multiple of 16 bytes, crashing PDF.load() entirely for corrupted encrypted PDFs. Now truncates to the nearest block boundary and decrypts what's available.

aesDecryptWithIv keeps its strict validation since it's used for key derivation where misalignment is a real error.

Note

decryptObject in document-parser.ts still lacks a general try-catch for other decryption failures (e.g. PKCS#7 padding errors). That's a pre-existing gap, not introduced here, but worth addressing separately.

Test plan

  • Truncation of misaligned ciphertext recovers valid data
  • Sub-block-size ciphertext returns empty instead of throwing

@vercel
Copy link
Contributor

vercel bot commented Feb 17, 2026

@ntedvs is attempting to deploy a commit to the mythie's projects Team on Vercel.

A member of the Team first needs to authorize it.

aesDecrypt now logs a warning when truncating misaligned ciphertext.
decryptObject catches all decryption errors (e.g. PKCS#7 padding) and
falls back to the raw object instead of crashing PDF.load().
@Mythie
Copy link
Contributor

Mythie commented Feb 19, 2026

Good stuff! 🙌🏻

@Mythie Mythie merged commit fde3b13 into LibPDF-js:main Feb 19, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slightly corrupted pdfs throw on parsing

2 participants

Comments